package it.infodea.tapestrydea.services;


import it.infodea.tapestrydea.services.jwindows.JsWindowsThemesAssetsProvider;
import it.infodea.tapestrydea.services.jwindows.impl.JsWindowsThemesAssetsProviderImpl;

import java.util.HashMap;
import java.util.Map;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.services.AssetSource;

/**
 * 
 * @author bobpuley
 *
 */
public class JsWindowsModule {

	/**
	 * In questo metodo sono definiti i servizi utilizzati all'interno
	 * dell'applicazione. In particolare, sono configurate le associazioni tra i
	 * servizi e le loro implementazioni.
	 * 
	 * @param binder
	 */
//	public static void bind(ServiceBinder binder) {
//		binder.bind(JsWindowsThemesAssetsProvider.class, JsWindowsThemesAssetsProviderImpl.class);
//	}

	
	public JsWindowsThemesAssetsProvider buildJsWindowsThemesAssetsProvider(Map<String, String> contributions, AssetSource source){
		Map<String, Asset[]> assetsMap = new HashMap<String, Asset[]>();
		for(String key: contributions.keySet()){
			assetsMap.put(key, getValues(contributions.get(key), source));
		}
		return new JsWindowsThemesAssetsProviderImpl(assetsMap, "dialog");
	}
	
	private Asset[] getValues(String paths, AssetSource source) {
		Asset[] result = new Asset[0];
		String[] splitted = paths.split(",");
		result = new Asset[splitted.length];
		int index = 0;
		for (String item : splitted) {
			result[index++] = source.getClasspathAsset(item);
		}
		return result;
	}


	/**
	 * Aggiunge nuovi tipi ai data type di base di Tapestry. The data type are
	 * used to select an editor (or display) block. Normally, the data type is
	 * determined from the type of the property (for example, property type
	 * java.lang.String would map to data type "text").
	 * 
	 * @param configuration
	 */
	public void contributeJsWindowsThemesAssetsProvider(MappedConfiguration<String, String> configuration) {
		configuration.add("bluelighting", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/lighting.css");
		configuration.add("darkX", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/darkX.css");
		configuration.add("alphacube", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/alphacube.css");
		configuration.add("spread", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/spread.css");
		configuration.add("nuncio", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/nuncio.css");
		configuration.add("mac_os_x", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/mac_os_x.css,/it/infodea/tapestrydea/assets/scripts/jswindows/themes/mac_os_x_dialog.css");
		configuration.add("dialog", "classpath:/it/infodea/tapestrydea/assets/scripts/jswindows/themes/default.css");
	}


}
